package com.lanou3g.code0509;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/9;
 * Otherwise, I don't know who created it either
 */

public class MySQLServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            Connection conn = DriverManager
                    .getConnection("jdbc:mysql://localhost:3306/lan_ou",
                    "root","123456");

            String sql = "SELECT * FROM stu";

            PreparedStatement statement = conn.prepareStatement(sql);

            ResultSet rs = statement.executeQuery();

            rs.next();// 移到数据的第一行

            String name = rs.getString("sname");
            Integer age = rs.getInt("age");

            writer.println(name);
            writer.println(age);

            rs.close();
            statement.close();
            conn.close();



        } catch (SQLException e) {
            e.printStackTrace();
        }

        writer.close();
    }
}
